package com.rhit.rosehack.SQLObjects;

import javax.swing.JOptionPane;
import java.sql.*;

/**
 * Utility class to load the default initial values 
 * into the database. This class will also clear 
 * any data that is in the database.
 * @author Neil
 *
 */
public class Database_Init 
{

	/**
	 * @param args
	 */
	public static String addPlayer() 
	{
		String username = "Neil_Test";

		// Create the new Player.
		try {
			CallableStatement user_exists = Query.getConnection().prepareCall(
					"{ ? = call rh_user_exists( ? ) }" );
			user_exists.registerOutParameter( 1, Types.INTEGER );
			
			while( true )
			{
				username = JOptionPane.showInputDialog( "New Username: " );
				
				if( username == null )
				{
					System.exit( 0 );
				}
				
				user_exists.setString( "player_name", username );
				user_exists.execute();
				
				if( user_exists.getInt( 1 ) == 1 ) 
				{
					JOptionPane.showMessageDialog( null, 
												   "That username exists.", 
												   "Error", 
												   JOptionPane.ERROR_MESSAGE );
					continue;
				}
				
				break;
			}
			
			CallableStatement s = Query.getConnection().prepareCall(
					"{ ? = call rh_create_user( '" + username + "' ) }" );
			s.registerOutParameter( 1, Types.INTEGER );
			s.execute();
			
			if( s.getInt( 1 ) == 1 )
			{
				System.out.println( "User '" + username + "' created successfully." );
			}
			else
			{
				System.out.println( "Failed to create user '" + username + "'." );
			}
		} catch( SQLException e ) {
			e.printStackTrace();
			System.exit( -1 );
		}
		
		return username;
	}

}
